package org.nudt.system.controller

import org.nudt.common.model.PageData
import org.nudt.common.model.ResponseData
import org.nudt.common.model.Type
import org.nudt.common.model.Video
import org.nudt.system.service.SysVideoService
import org.springframework.data.domain.PageRequest
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/v1/system/video")
class SysVideoController(private val sysVideoService: SysVideoService) {
    
    /**
     * 保存视频信息，如果视频ID已存在则不更新
     */
    @PostMapping("/saveNotUpdate")
    fun saveVideo(@RequestBody video: Video): ResponseData<String> {
        // 调用sysVideoService的saveNotUpdate方法保存视频信息
        sysVideoService.saveNotUpdate(video)
        return ResponseData.success("success")
    }
    
    @GetMapping("/{id}")
    fun getVideoById(@PathVariable id: Int): ResponseData<Video?> {
        val video = sysVideoService.getVideoById(id)
        return ResponseData.success(video)
    }
    
    @GetMapping("/type")
    fun getTypeList(): ResponseData<MutableList<Type>> {
        return ResponseData.success(sysVideoService.getVideoTypeList())
    }
    
    /**
     * 获取视频列表分页
     */
    @GetMapping("/list")
    fun getVideoList(
        @RequestParam page: Int, @RequestParam size: Int,
        @RequestParam(name = "videoName", required = false) videoName: String?,
        @RequestParam(name = "actor", required = false) actor: String?,
        @RequestParam(name = "area", required = false) area: String?,
        @RequestParam(name = "year", required = false) year: String?,
    ): ResponseData<PageData<Video?>> {
        val pageResult = sysVideoService.getVideoList(videoName, actor, area, year, PageRequest.of(page, size))
        
        return ResponseData.success(pageResult)
    }
    
    @PostMapping("/{id}/delete")
    fun deleteVideo(@PathVariable id: Int): ResponseData<String> {
        sysVideoService.deleteVideoById(id)
        return ResponseData.success("")
    }
    
    @PostMapping("/update")
    fun updateVideo(@RequestBody video: Video): ResponseData<Video> {
        val videoUpdated = sysVideoService.updateVideo(video)
        return ResponseData.success(videoUpdated)
    }
    
    @PostMapping("/update/{id}/cover")
    fun updateVideoCover(@PathVariable id: Int): ResponseData<Video?> {
        val video = sysVideoService.getVideoById(id)
        
        return ResponseData.success(video)
    }
}